package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.business.domain.Station;
import com.myc.train.business.domain.StationExample;
import com.myc.train.business.resp.StationQueryResp;
import com.myc.train.common.exception.BusinessException;
import com.myc.train.common.exception.BusinessExceptionEnum;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.TrainStation;
import com.myc.train.business.domain.TrainStationExample;
import com.myc.train.business.mapper.TrainStationMapper;
import com.myc.train.business.req.TrainStationQueryReq;
import com.myc.train.business.req.TrainStationSaveReq;
import com.myc.train.business.resp.TrainStationQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainStationService {

    @Resource
    private TrainStationMapper trainStationMapper;

    public void save(TrainStationSaveReq req){
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);

        if(ObjectUtil.isNull(trainStation.getId())){
            //唯一键校验
            TrainStation trainStation1 = selectByUnique(req.getTrainCode(), req.getIndex());
            if(ObjectUtil.isNotEmpty(trainStation1)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
            }
            trainStation1 = selectByUnique(req.getTrainCode(),req.getName());
            if(ObjectUtil.isNotEmpty(trainStation1)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
            }
            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        }else{
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKey(trainStation);
        }
    }

    //唯一键校验
    public TrainStation selectByUnique(String trainCode, Integer index){
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode).andIndexEqualTo(index);
        List<TrainStation> trainStations = trainStationMapper.selectByExample(trainStationExample);
        if(CollUtil.isNotEmpty(trainStations)){
            return trainStations.get(0);
        }else {
            return null;
        }
    }
    public TrainStation selectByUnique(String trainCode, String name){
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode).andNameEqualTo(name);
        List<TrainStation> trainStations = trainStationMapper.selectByExample(trainStationExample);
        if(CollUtil.isNotEmpty(trainStations)){
            return trainStations.get(0);
        }else {
            return null;
        }
    }

    public PageResp<TrainStationQueryResp> queryList(TrainStationQueryReq req){
       //定义查询条件
        TrainStationExample trainStationExample = new TrainStationExample();
        //根据id倒序排序
        trainStationExample.setOrderByClause("train_code asc , `index` asc");
        //创建查询对象
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<TrainStation> trainStations = trainStationMapper.selectByExample(trainStationExample);
        //new一个PageInfo并将结果放进来
        PageInfo<TrainStation> trainStationPageInfo = new PageInfo<>(trainStations);
        //类拷贝
        List<TrainStationQueryResp> queryResps = BeanUtil.copyToList(trainStations, TrainStationQueryResp.class);
        //new一个PageResp
        PageResp<TrainStationQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(trainStationPageInfo.getTotal());
        resp.setList(queryResps);
        return resp;
    }

    public void delete(Long id){
        trainStationMapper.deleteByPrimaryKey(id);
    }

    public List<TrainStation> selectByCode(String trainCode){
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("`index` asc");
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainStationMapper.selectByExample(trainStationExample);
    }
}
